package com.enqualcomm.kids.activities;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.enqualcomm.kids.BuildConfig;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;

public class AddWatchActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_watch);
        initView();
    }

    private void initView() {
        findViewById(R.id.title_bar_left_iv).setOnClickListener(this);
        TextView title_bar_name=(TextView) findViewById(R.id.title_bar_title_tv);
        title_bar_name.setText("手表型号");


        int[] ids = new int[2];
        if ("一米阳光".equals(BuildConfig.COMPANY)){
            ids[0] = R.drawable.product_yimi_g1;
            ids[1] = R.drawable.product_yimi_g2;
        }else {
            ids[0] = R.drawable.product_w03;
            ids[1] = R.drawable.product_w13;
        }

        ListView listview = (ListView) findViewById(R.id.listview);
        listview.setAdapter(new MyAdapter(ids,this));
        listview.setOnItemClickListener(this);
    }


    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch ((int)id){
            case R.drawable.product_yimi_g1:
            case R.drawable.product_w03:
//                startActivity(AddWatchWithWidActivity.class);
                startActivity(AddWatchWidActivity_.class);
                finish();
                break;

            case R.drawable.product_yimi_g2:
            case R.drawable.product_w13:
//                startActivity(AddWatchWithCidActivity.class);
                startActivity(AddWatchCidActivity_.class);
                finish();
                break;
        }
    }

    static class MyAdapter extends BaseAdapter {
        private int resIds[];
        private final LayoutInflater inflater;

        public MyAdapter(int[] resIds,Context context) {
            this.resIds = resIds;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return resIds!=null?resIds.length:0;
        }

        @Override
        public Object getItem(int position) {
            return resIds[position];
        }

        @Override
        public long getItemId(int position) {
            return resIds[position];
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv ;
            if (convertView==null){
                convertView = inflater.inflate(R.layout.lv_item_products,null);
                iv = (ImageView) convertView.findViewById(R.id.iv_icon);
                convertView.setTag(iv);
            }else {
                iv = (ImageView) convertView.getTag();
            }

            iv.setImageResource(resIds[position]);
            return convertView;
        }
    }
}
